package com.zany.children.action;

import com.zany.children.service.ClassService;
import com.zany.children.service.StudentService;
import com.zany.children.vo.Class;
import com.zany.children.vo.Student;
import com.zany.children.vo.Teacher;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentAction extends AbstractActionSupport {

    private Map session;


    public ClassService getClassService() {
        return classService;
    }

    public void setClassService(ClassService classService) {
        this.classService = classService;
    }

    public StudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    private ClassService classService;
    private StudentService studentService;

    private static Log logger = LogFactory.getLog(StudentAction.class);
    public void save() throws ZanyException {
        response = ServletActionContext.getResponse();
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String identityCode = request.getParameter("identityCode");
        String parentTel = request.getParameter("parentTel");
        String address = request.getParameter("address");
        String classId = request.getParameter("classId");

        Student student = new Student();
        UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
        String orgCode = null;
        if (userInfo != null) {
            orgCode = userInfo.getUserOrg();
            student.setCreater(userInfo.getUserName());
            student.setOrgCode(orgCode);
        }
        student.setName(name);
        student.setAddress(address);
        student.setParentTel(parentTel);
        student.setSex(sex);
        student.setIdentityCode(identityCode);
        student.setAge(Integer.parseInt(age));
        studentService.saveStudent(student,Long.parseLong(classId));
        AjaxTools.exAjax("{success:true}", this.response);
    }
    public void delete() throws ZanyException {
        if (logger.isInfoEnabled()) {
            logger.info("StudentAction.delete()");
        }
        response = ServletActionContext.getResponse();
        String delId[] = request.getParameterValues("delId");
        try {
            if (delId.length > 0) {
                for (int i = 0; i < delId.length; i++) {
                    studentService.deleteStudentById(Long.parseLong(delId[i]));
                }
                AjaxTools.exAjax("{success:true}", response);
            }
        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }

    }
    public void update() throws ZanyException {
        response = ServletActionContext.getResponse();
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String identityCode = request.getParameter("identityCode");
        String parentTel = request.getParameter("parentTel");
        String address = request.getParameter("address");
        String classId = request.getParameter("classId");
        String id = request.getParameter("id");
        Student student = new Student();
        student.setId(Long.parseLong(id));
        student.setName(name);
        student.setAddress(address);
        student.setParentTel(parentTel);
        student.setSex(sex);
        student.setIdentityCode(identityCode);
        student.setAge(Integer.parseInt(age));

        Class clazz = classService.getClassById(Long.parseLong(classId));
        student.setClazz(clazz);
        studentService.updateStudent(student);
        AjaxTools.exAjax("{success:true}", response);
    }
    public void list() throws ZanyException {
        String start = request.getParameter("start");
        String classId = request.getParameter("classId");
        String name = request.getParameter("name");
        UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
        response = ServletActionContext.getResponse();
        Integer pageStart = StringUtils.isBlank(start) ? 1 : Integer.parseInt(start);
        Teacher teacher = new Teacher();
        teacher.setPageMessage(pageStart);
        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            map.put("orgCode", userInfo.getUserOrg());
        }
        map.put("classId", classId);
        map.put("name", name);
        map.put("pageStart", pageStart);
        map.put("pageEnd", teacher.getPageEnd());
        Integer pageTotal = studentService.getStudentListPageCount(map);
        List<Student> studentListPage = studentService.getStudentListPage(map);
        for(Student stu:studentListPage){
            if(stu.getClazz()!=null) {
                stu.setClassName(stu.getClazz().getName());
                stu.setClassId(stu.getClazz().getId());
            }
        }
        AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(studentListPage) + "}", response);
    }

    @Override
    public void setSession(Map map) {
        this.session = map;
    }
}
